package com.ruoyi.project.gxgw_modules.goods.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.gxgw_modules.goods.domain.Goods;
import com.ruoyi.project.gxgw_modules.goods.service.IGoodsService;
import com.ruoyi.project.gxgw_modules.gtype.domain.Gtype;
import com.ruoyi.project.gxgw_modules.gtype.service.IGtypeService;
import com.ruoyi.project.gxgw_modules.input.domain.Input;
import com.ruoyi.project.gxgw_modules.input.service.IInputService;
import com.ruoyi.project.gxgw_modules.output.domain.Output;
import com.ruoyi.project.gxgw_modules.output.service.IOutputService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * 商品 信息操作处理
 *
 * @author ruoyi
 * @date 2019-01-20
 */
@Controller
@RequestMapping("/gxgw_modules/goods")
public class GoodsController extends BaseController {
	private String prefix = "gxgw_modules/goods";

	@Autowired
	private IGoodsService goodsService;

	@Autowired
	private IGtypeService gtypeService;

	@Autowired
	private IInputService inputService;

	@Autowired
	private IOutputService outputService;

	@RequiresPermissions("gxgw_modules:goods:view")
	@GetMapping()
	public String goods() {
		return prefix + "/goods";
	}

	/**
	 * 查询商品列表
	 */
	@RequiresPermissions("gxgw_modules:goods:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Goods goods) {
		startPage();
		List<Goods> list = goodsService.selectGoodsList(goods);
		return getDataTable(list);
	}

	@RequiresPermissions("gxgw_modules:goods:list")
	@PostMapping("/selectGtypeName")
	@ResponseBody
	public List<Gtype> selectAdsName() {

		List<Gtype> gtype = gtypeService.selectGtypeName();

		return gtype;
	}

	/**
	 * 导出商品列表
	 */
	@RequiresPermissions("gxgw_modules:goods:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(Goods goods) {
		List<Goods> list = goodsService.selectGoodsList(goods);
		ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
		return util.exportExcel(list, "goods");
	}

	/**
	 * 新增商品
	 */
	@GetMapping("/add")
	public String add() {

		return prefix + "/add";
	}

	/**
	 * 新增保存商品
	 */
	@RequiresPermissions("gxgw_modules:goods:add")
	@Log(title = "商品", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Goods goods) {

		return toAjax(goodsService.insertGoods(goods));
	}

	/**
	 * 新增商品入库申请
	 *
	 */
	@RequiresPermissions("gxgw_modules:goods:addInput")
	@Log(title = "商品入库", businessType = BusinessType.INSERT)
	@PostMapping("/addInput")
	@ResponseBody
	public AjaxResult addSave(Input input) {
		Date date = new Date();
		Timestamp timeStamp = new Timestamp(date.getTime());
		input.setInputTime(timeStamp);
		Long id = ShiroUtils.getSysUser().getUserId();
        
		input.setAdminId(id.intValue());

		return toAjax(inputService.insertInput(input));
	}

	/**
	 * 新增商品出库申请
	 */

	@RequiresPermissions("gxgw_modules:goods:addOutput")
	@Log(title = "商品出库", businessType = BusinessType.INSERT)
	@PostMapping("/addOutput")
	@ResponseBody
	public AjaxResult addSave(Output output) {
		Date date = new Date();
		Timestamp timeStamp = new Timestamp(date.getTime());
		output.setOutputTime(timeStamp);
		Long id = ShiroUtils.getSysUser().getUserId();
		output.setAdminId(id.intValue());
		return toAjax(outputService.insertOutput(output));
	}

	/**
	 * 修改商品
	 */
	@GetMapping("/edit/{goodsId}")
	public String edit(@PathVariable("goodsId") Integer goodsId, ModelMap mmap) {
		Goods goods = goodsService.selectGoodsById(goodsId);
		mmap.put("goods", goods);
		return prefix + "/edit";
	}

	@GetMapping("/addInput/{goodsId}")
	public String addInput(@PathVariable("goodsId") Integer goodsId, ModelMap mmap) {
		Goods goods = goodsService.selectGoodsById(goodsId);
		mmap.put("goods", goods);
		return prefix + "/addInput";
	}

	@GetMapping("/addOutput/{goodsId}")
	public String addOutput(@PathVariable("goodsId") Integer goodsId, ModelMap mmap) {
		Goods goods = goodsService.selectGoodsById(goodsId);
		mmap.put("goods", goods);
		return prefix + "/addOutput";
	}

	/**
	 * 修改保存商品
	 */
	@RequiresPermissions("gxgw_modules:goods:edit")
	@Log(title = "商品", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Goods goods) {
		return toAjax(goodsService.updateGoods(goods));
	}

	/**
	 * 删除商品
	 */
	@RequiresPermissions("gxgw_modules:goods:remove")
	@Log(title = "商品", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		String[] id = ids.split(",");

		for (int i = 0; i < id.length; i++) {
			Integer goodsIds = Integer.parseInt(id[i]);
			if (goodsService.selectNum(goodsIds) > 0)
				return error(1, "库存不为0的商品无法删除!");
			if (goodsService.checkInputRecord(goodsIds) > 0) {

				return error(1, "该商品已有出入库记录,不允许删除,若要删除，请先删除相关记录！");
			}

			if (goodsService.checkOutputRecord(goodsIds) > 0) {

				return error(1, "该商品已有出入库记录,不允许删除,若要删除，请先删除相关记录！");
			}

		}
		return toAjax(goodsService.deleteGoodsByIds(ids));
	}

	/**
	 * 检查名称唯一性
	 */
	@PostMapping("/checkGoodsNameUnique")
	@ResponseBody
	public String checkGoodsNameUnique(Goods goods) {

		return goodsService.checkGoodsNameUnique(goods);
	}

	/**
	 * 检查商品条码唯一性
	 */
	@PostMapping("/checkGoodsBarUnique")
	@ResponseBody
	public String checkGoodsBarUnique(Goods goods) {

		return goodsService.checkGoodsBarUnique(goods);
	}
}
